<?php

namespace App\Models;

use App\Utils\ConstantUtil;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'users';

    protected $appends = [
        'status_name' ,
    ];

    public function getStatusNameAttribute()
    {
        switch ($this->status) {
            case ConstantUtil::USER_STATUS_TRUE:
                return "启用";
                break;
            case ConstantUtil::USER_STATUS_FALSE :
                return "禁用";
                break;
        }
    }
    public static function getUserRole($id) {
        if ($id == 1) return '超级管理员';
        $list = AuthRelation::join("auth_roles","auth_roles.id","=","auth_relations.group_id")
            ->where("auth_relations.uid",$id)
            ->where("auth_roles.status",1)
            ->select("auth_roles.title")
            ->get()->toArray();
        if (empty($list)) return '';
        $titles = array_column($list, "title");
        return implode(' | ', $titles);
    }

    public  function create(){
        $exist = User::where("name",$this->name)->first();
        if(!empty($exist)) return false;
        $this->save();
        $account = new Account();
        $account->user_id = $this->id;
        $account->save();
        return $this->id;
    }

}
